package com.box.service.impl.slorjimpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.box.entity.solrjItem.SearchBlog;
import com.box.mapper.DbBlogMapper;
import com.box.mapper.SolrjMapper;
import com.box.service.solrj.SolrjService;
import com.box.utils.PageView;
import com.box.utils.ResponseResult;


/**   
*    
* 项目名称：MyBox   
* 类名称：SolrjServiceImpl   
* 类描述：   搜索实现类
* 创建人：君无邪   
* 创建时间：2019年11月27日 下午10:24:34   
* 修改备注：  https://www.java214.com 
* @version    1.0
*    
*/ 
@Service
public class SolrjServiceImpl implements SolrjService {
	
	@Autowired
	private SolrServer solr;
	
	@Autowired
	private SolrjMapper mapper;

	public PageView searchBySolrj(SolrQuery query) throws Exception {
		// TODO Auto-generated method stub
		QueryResponse response = solr.query(query);
		//获取查询结果列表
		SolrDocumentList solrDomList = response.getResults();
		List<SearchBlog> blogList=new ArrayList<SearchBlog>();
		//列表被转换为对象进行遍历输出
		for (SolrDocument dom : solrDomList) {
			//创建一个对象searchitem对象
			SearchBlog blog=new SearchBlog();

			blog.setId((String) dom.get("id"));
			blog.setImageUrl((String) dom.get("blog_imageUrl"));
			blog.setSummary((String) dom.get("blog_summary"));
			blog.setKeyword((String) dom.get("blog_keyword"));
			blog.setContent((String) dom.get("blog_content"));
			//取高亮显示
			Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
			List<String> list = highlighting .get(dom.get("id")).get("blog_title");
			String itemTitle="";
			if(list!=null&&list.size()>0){
				//获取高亮后的结果
				itemTitle= list.get(0); 
				
			}else{
				itemTitle=(String) dom.get("blog_title");
			}
			blog.setTitle(itemTitle);
			//添加到搜索参数列表中
			blogList.add(blog);
		}
		PageView result=new PageView();
		result.setBlogList(blogList);
		result.setRecordCount(solrDomList.getNumFound());
		return result;
	}

	public ResponseResult updateSearchBlogById(Integer blogId) throws Exception {
		// TODO Auto-generated method stub
		//查询记录
		SearchBlog blog = mapper.getSearchBlogById(blogId);
		//记录更新
		SolrInputDocument document=new SolrInputDocument();
		//向文档对象添加域
document.addField("id", blog.getId());//获取id到索引库
document.addField("blog_title", blog.getTitle());//获取标题到索引库
document.addField("blog_summary",blog.getSummary());//摘要
document.addField("blog_imageUrl", blog.getImageUrl());//图片
document.addField("blog_keyword", blog.getKeyword());//关键字
document.addField("blog_content", blog.getContent());//博客内容
//向索引库添加文档
solr.add(document);
//提交
solr.commit();
		return ResponseResult.ok();
	}

	@Override
	public ResponseResult deleteSearchBlogById(Integer blogId) throws Exception {
		//删除记录

        try{
            SearchBlog blog = mapper.getSearchBlogById(blogId);
            if(blog!=null){//如果查询的数据不为空,则删除对应数据
                solr.deleteById(blog.getId());
                solr.commit();
                return ResponseResult.ok();
            }
            //为空，则直接删除索引数据
            solr.deleteById(blogId.toString());
            solr.commit();
            return ResponseResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.build(500,"系统错误",e);
        }

	}


}
